//
//  RSVisitor.swift
//  RSWeibo
//
//  Created by Macx on 16/8/8.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit
import SnapKit

class RSVisitor: UIView {
    
    //  延时初始化
    var loginClosure: (()->())?
    
    // MARK: 懒加载控件
    // 旋转图片
    private lazy var cycleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //  主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //  消息信息
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        //  设置文字居中
        label.textAlignment = .Center
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    //  注册
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    //  登陆
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("登陆", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()


    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  开启动画
    private func startAnimation() {
        
        //  创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  动画目的地
        animation.toValue = 2 * M_PI
        //  动画时长
        animation.duration = 20
        //  执行次数
        animation.repeatCount = MAXFLOAT
        //  动画执行完成不让其释放
        animation.removedOnCompletion = false
        //  执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //  修改访客视图信息
    func updateVisitorViewInfo(message: String?, imageName: String?) {
        
        if let msg = message, imgName = imageName {
            //  消息,发现,我的设置的访客视图信息
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            //  隐藏旋转图片
            cycleImageView.hidden = true
        } else {
            //  表示首页要设置的访客视图情况
            //  显示旋转图片
            cycleImageView.hidden = false
            //  只有首页开启旋转动画
            startAnimation()
        }
        
        
    }
    
    
    
    
    // 私有方法 设置界面
    private func setupUI(){
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 添加子控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置约束
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.height.equalTo(35)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }

    
        
    }
    
    //  MARK: --添加点击事件
    @objc private func registerButtonAction() {

        //  执行闭包
        loginClosure?()
        
    }
    
    @objc private func loginButtonAction() {

        //  执行闭包
        loginClosure?()
        
    }

    
}


extension RSVisitor{

    // 使用系统的方法 设置约束
//     cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//    
//    addConstraint(NSLayoutConstraint(item: cycleImageView, attribute:NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//    addConstraint(NSLayoutConstraint(item: cycleImageView, attribute:NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
//    
}


















